﻿using Mt.Site.Core.Configuration;
using Mt.Site.Core.ContentManagement.Permissions;
using Mt.Site.Core.CustomConfigurations;
using Mt.Site.Core.Navigations.Entities;
using Mt.Site.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Mt.Site.Migrations.Seed.Host
{
    /// <summary>
    /// 添加一些基础的配置数据
    /// </summary>
    public class SomeBasicDataCreator
    {
        private readonly MtSiteDbContext _context;

        public SomeBasicDataCreator(MtSiteDbContext context)
        {
            _context = context;
        }

        public void Create()
        {
            //添加一些常用的自定义配置（如图片上传，附件上传等）
            CreateConfigs();

            //添加一些常用的模块配置（比如首页配置）
            CreateModules();
        }

        private void CreateConfigs()
        {
            #region  水印图片 

            CreateConfigIfNotExists(
                Guid.Parse("C392153A-4994-4EED-B57E-1E956122C307"),
                "水印图片（添加水印时使用的图片（水印））",
                "waterMark" ,
                "水印图片的上传配置",
                @"
                {
                    ""Alias"": ""tp"",
                    ""IsPic"": true,
                    ""Description"": {
                        ""Default"": ""单个文件最大1M。可上传类型 *.JPG、*.PNG、*.JPEG"",
                        ""Values"": {
                            ""en"": ""单个文件最大1M。可上传类型 *.JPG、*.PNG、*.JPEG"",
                            ""zh-CN"": ""单个文件最大1M。可上传类型 *.JPG、*.PNG、*.JPEG""
                        }
                    },
                    ""UpFileInfo"": {
                        ""Directory"": ""WaterMark"",
                        ""ExNames"": [
                            ""jpg"",
                            ""jpeg"",
                            ""png""
                        ],
                        ""MaxSize"": 1,
                        ""ImageSets"": [],
                        ""DeleteOriginalImage"": false
                    },
                    ""PermissionName"": """"
                }"
            , CustomConfigDataType.SingleFile);

            #endregion

            #region Ckeditor

            CreateConfigIfNotExists(
                Guid.Parse("C9E92F55-CCE0-459A-ABD4-45B92DC5B668"),
                "Ckeditor的图片和文件的上传配置",
                "ckeditor",
                "富文本编辑器中的图片和附件上传的规则",
                @"
                {
                    ""ModuleList"": [
                        {
                            ""Alias"": ""Images"",
                            ""IsPic"": true,
                            ""UpFileInfo"": {
                                ""Directory"": ""Ckeditor/Images"",
                                ""ExNames"": [
                                    ""jpg"",
                                    ""jpeg"",
                                    ""gif"",
                                    ""png""
                                ],
                                ""MaxSize"": 10,
                                ""UseOriginalName"":true,
                                ""OpWhenHasExisting"": 0,
                                ""ImageSets"": [],
                                ""DeleteOriginalImage"": false
                            }
                        },
                
                        {
                            ""Alias"": ""Files"",
                            ""IsPic"": false,
                            ""UpFileInfo"": {
                                ""Directory"": ""Ckeditor/Files"",
                                ""ExNames"": [
                                    ""jpg"",
                                    ""jpeg"",
                                    ""png"",
                                    ""gif"",
                                    ""bmp"",
                                    ""pdf"",
                                    ""rar"",
                                    ""zip"",
                                    ""doc"",
                                    ""docx"",
                                    ""wps"",
                                    ""ppt"",
                                    ""pptx"",
                                    ""txt"",
                                    ""rtf"",
                                    ""md"",
                                    ""xls"",
                                    ""xlsx"",
                                    ""mp4"",
                                    ""avi"",
                                    ""ogv"",
                                    ""webm"",
                                    ""flv"",
                                    ""f4v"",
                                    ""wmv"",
                                    ""mp3"",
                                    ""ogg"",
                                    ""wav"",
                                    ""wma"",
                                    ""mid""
                                ],
                                ""MaxSize"": 30,
                                ""UseOriginalName"":true,
                                ""OpWhenHasExisting"": 0,
                                ""ImageSets"": [],
                                ""DeleteOriginalImage"": false
                            }
                        }
                    ]
                }"
            , CustomConfigDataType.MultiFiles);

            #endregion

            #region 单文件上传控件的默认配置

            #region 默认单文件图片上传配置

            CreateConfigIfNotExists(
                Guid.Parse("78D56065-E814-47D3-8B49-AA965AFFB15C"),
                "默认单文件图片上传配置", "singlePictureUpload",
                "默认的上传配置",
                @"
                {
                    ""Alias"": ""slt"",
                    ""IsPic"": true,
                    ""Description"": {
                        ""Default"": ""单个文件最大10M。可上传类型 *.JPG、*.GIF、*.PNG、*.JPEG。"",
                        ""Values"": {
                            ""en"": ""单个文件最大10M。可上传类型 *.JPG、*.GIF、*.PNG、*.JPEG。"",
                            ""zh-CN"": ""单个文件最大10M。可上传类型 *.JPG、*.GIF、*.PNG、*.JPEG。""
                        }
                    },
                    ""UpFileInfo"": {
                        ""Directory"": ""single"",
                        ""ExNames"": [
                            ""jpg"",
                            ""jpeg"",
                            ""gif"",
                            ""png""
                        ],
                        ""MaxSize"": 10,
                        ""ImageSets"": [],
                        ""DeleteOriginalImage"": true
                    },
                    ""PermissionName"": """"
                }"
            , CustomConfigDataType.SingleFile);

            #endregion

            #region 默认单上传文件配置

            CreateConfigIfNotExists(
                Guid.Parse("7F8814C5-D6A7-497B-BF92-D0911CF02F79"),
                "默认单上传文件配置", "singleFileUpload" ,
                "文件上传的基本配置",
                @"
                {
                    ""Alias"": ""fj"",
                    ""IsPic"": false,
                    ""ALterFileName"":true,
                    ""Description"": {
                        ""Default"": ""单个文件最大30M。可上传类型 *.pdf,*.zip。"",
                        ""Values"": {
                            ""en"": ""单个文件最大30M。可上传类型 *.pdf,*zip。"",
                            ""zh-CN"": ""单个文件最大30M。可上传类型 *.pdf。""
                        }
                    },
                    ""UpFileInfo"": {
                        ""Directory"": ""single"",
                        ""ExNames"": [
                            ""jpg"",
                            ""jpeg"",
                            ""png"",
                            ""gif"",
                            ""bmp"",
                            ""pdf"",
                            ""rar"",
                            ""zip"",
                            ""doc"",
                            ""docx"",
                            ""wps"",
                            ""ppt"",
                            ""pptx"",
                            ""txt"",
                            ""rtf"",
                            ""md"",
                            ""xls"",
                            ""xlsx"",
                            ""mp4"",
                            ""avi"",
                            ""ogv"",
                            ""webm"",
                            ""flv"",
                            ""f4v"",
                            ""wmv"",
                            ""mp3"",
                            ""ogg"",
                            ""wav"",
                            ""wma"",
                            ""mid""
                        ],
                        ""MaxSize"": 30,
                        ""ImageSets"": [],
                        ""UseOriginalName"":true,
                        ""DeleteOriginalImage"": true
                    },
                    ""PermissionName"": """"
                }"
            , CustomConfigDataType.SingleFile);

            #endregion

            #endregion
        }

        private void CreateModules()
        {
            AddHomeIndex();

            AddMenuInfo();

            //AddContent();
        }

        private void CreateConfigIfNotExists(Guid id, string name, string key, string description, string ExtensionData, string dataType)
        {
            var config = _context.CustomConfigurations.FirstOrDefault(c => c.Id == id);
            if (config == null)
            {
                _context.CustomConfigurations.Add(new CustomConfiguration
                {
                    Id = id,
                    Name = name,
                    Key = key,
                    Description = description,
                    ExtensionData = ExtensionData,
                    DataType = dataType
                });
            }
        }

        private void AddHomeIndex()
        {
            var module = new BackEndModule
            {
                Id = Guid.Parse("9BDE5674-3C01-47DA-8324-6BB9056DF137"),
                IsActive = true,
                Name = ContentPermissionProvider.ModuleName,
                DisplayName = "首页",
                Url = "welcome",
                Order = 1
            };

            // ModuleFeatureSetting

            // Page
            var pages = new List<FrontEndPage>();
            var page1 = new FrontEndPage
            {
                Id = Guid.Parse("9C0C7392-35D4-466D-B8C1-8BA1EA6D8FA4"),
                IsActive = true,
                Name = "首页",
                Order = 1,
                BackEndModuleId = module.Id,
                ControllerName = "Home",
                ActionName = "Index"
            };
            pages.Add(page1);

            CreateModuleIfNotExists(module, pages);
        }
        private void AddMenuInfo()
        {
            var module = new BackEndModule
            {
                Id = Guid.Parse("3B8A679C-CA84-4CB6-85A6-39968919DB54"),
                IsActive = true,
                Name = null,
                DisplayName = "栏目信息管理",
                Url = "menusInfo",
                Icon = "fa-bars",
                Order = 2
            };

            CreateModuleIfNotExists(module, null);
        }
        private void AddContent()
        {
            //var module = new BackEndModule
            //{
            //    Id = Guid.Parse("5F8A679C-CA84-4CB6-85A6-39968919DB54"),
            //    IsActive = true,
            //    Name = "内容管理",
            //    Url = "content",
            //    Icon = "fa-sticky-note-o",
            //    Order = 3,
            //    CustomData = "defaultInfo"
            //};

            //// ModuleFeatureSetting
            //module.RequiresAll = true;
            //var featureSetting = new ModuleFeatureSetting(module.Id, ContentFeatures.ContentFeature, "True")
            //{
            //    Id = Guid.Parse("f6120796-02dd-4583-86fb-5cb55b817a60")
            //};

            //CreateModuleIfNotExists(module, featureSetting, null);
        }

        private void CreateModuleIfNotExists(BackEndModule module, List<FrontEndPage> pages)
        {
            if (_context.BackEndModules.FirstOrDefault(c => c.Id == module.Id) == null)
            {
                _context.BackEndModules.Add(module);
                _context.SaveChanges();
            }

            //页面
            if(pages != null)
            {
                foreach (var page in pages)
                {
                    if (_context.FrontEndPages.FirstOrDefault(c => c.Id == page.Id) == null)
                    {
                        _context.FrontEndPages.Add(page);
                    }
                }
            }
        }
    }
}
